package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.kwq.project_manger.aop.annotations.LimitRequest;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.pojo.dto.DelFileDto;
import cn.kwq.project_manger.service.FilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags ="文件服务")
@RestController
@RequestMapping("/file/")
public class FilesController {

    @Autowired
    FilesService filesService;


    @ApiOperation("上传头像(暂时不用)")
    @SaCheckLogin
    @PostMapping("uploadImage")
    public RES uploadImge(@RequestPart @RequestParam("file") MultipartFile file) throws Exception {
        String url = filesService.uploadImage(file, StpUtil.getLoginIdAsLong());
        return R.ok("上传成功",url);
    }

    @ApiOperation("上传项目图片（多文件）")
    @SaCheckLogin
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件流对象,接收数组格式", required = true,dataType = "MultipartFile",allowMultiple = true),
            @ApiImplicitParam(name = "projectId", value = "项目或比赛id", required = true)
    })
    @PostMapping("addProJImages")
    public RES addProJImages(@RequestPart @NotNull("没有上传文件") @RequestParam(value="files",required = true) MultipartFile[] files,
                            @RequestParam("projectId") Long projectId) throws Exception {
        if (ArrayUtil.isEmpty(files)){
            throw new RuntimeException("上传文件为空");
        }

        List<String> urls=new ArrayList<>();
        for (MultipartFile f:files){
            String url = filesService.addProJImage(f,StpUtil.getLoginIdAsLong(),projectId);
            urls.add(url);
        }

        return R.ok("上传成功",urls);
    }


    @ApiOperation("上传项目图片（单文件）")
    @SaCheckLogin
    @LimitRequest(time = 100,count = 10,msg = "请求过快")//100毫秒只能请求10次
    @PostMapping("addProJImage")
    public RES addProJImage(@RequestPart @NotNull("没有上传文件") @RequestParam(value="file") MultipartFile file,
                            @RequestParam("projectId") Long projectId) throws Exception {

        return R.ok("上传成功",filesService.addProJImage(file,StpUtil.getLoginIdAsLong(),projectId));
    }

    @ApiOperation("删除项目图片")
    @SaCheckLogin
    @PostMapping("delProJImage")
    public RES delProJImage(@RequestBody DelFileDto dto) throws Exception {
        filesService.delImage(dto.getUrl(),dto.getProjectId());
        return R.ok("删除成功");
    }





}
